package cn.lj.controller;


import cn.lj.common.handler.BusinessException;
import cn.lj.common.response.Result;
import cn.lj.common.response.ResultCode;
import cn.lj.entity.ComparedVo;
import cn.lj.entity.FinanceInfo;
import cn.lj.entity.FinanceInfoVo;
import cn.lj.entity.HistoryVo;
import cn.lj.service.FinanceInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李津
 * @since 2021-04-21
 */
@RestController
@RequestMapping("//finance-info")
@Api(value = "经费管理", tags = "经费管理接口")
public class FinanceInfoController {
    @Autowired
    private FinanceInfoService financeInfoService;


    /**
     * 条件查询新经费数据列表分页
     * @param financeInfoVo
     * @param current
     * @param size
     * @return
     */
    @PostMapping("/findFinanceInfoPage")
    @ApiOperation(value = "分页查询新经费数据列表",notes = "分页查询新经费数据列表")
    public Result findFinanceInfoPage(@RequestParam(required = true,defaultValue = "1")Integer current, @RequestParam(required = true,defaultValue = "8")Integer size
            ,@RequestBody FinanceInfoVo financeInfoVo){
        //对用户进行分页,泛型中注入的就是用户实体类
        Page<FinanceInfo> page = new Page<>(current,size);

        QueryWrapper<FinanceInfo> queryWrapper = getWrapper(financeInfoVo);

        //执行查询
        IPage<FinanceInfo> financeInfoPage = financeInfoService.findFinanceInfoPage(page,queryWrapper);

        long total = financeInfoPage.getTotal();
        List<FinanceInfo> records = financeInfoPage.getRecords();
        return Result.ok().data("total", total).data("records",records);
    }

    /**
     * QueryWrapper条件构造器
     * 将查询条件封装起来
     * @param financeInfoVo
     * @return
  */
    private QueryWrapper<FinanceInfo> getWrapper(FinanceInfoVo financeInfoVo) {
        QueryWrapper<FinanceInfo> queryWrapper = new QueryWrapper<>();
        if (financeInfoVo != null) {
            //绝对查询,查询逻辑为删除的
            queryWrapper.eq("is_deleted",0);
/*            if (!ObjectUtils.isEmpty(financeInfoVo.getUserId())) {
                queryWrapper.eq("claimer_user_id", financeInfoVo.getUserId());
            }*/
            //模糊查询
            if (!ObjectUtils.isEmpty(financeInfoVo.getReason())) {
                queryWrapper.like("reason", financeInfoVo.getReason());
            }
            if (!ObjectUtils.isEmpty(financeInfoVo.getUserName())) {
                queryWrapper.like("user_name", financeInfoVo.getUserName());
            }
            //时间端查询
            if (!ObjectUtils.isEmpty(financeInfoVo.getStartTime())) {
                //queryWrapper.ge大于等于 >=
                queryWrapper.ge("reason_time", financeInfoVo.getStartTime());
            }
            if (!ObjectUtils.isEmpty(financeInfoVo.getEndTime())) {
                //queryWrapper.le小于 <
                queryWrapper.le("reason_time", financeInfoVo.getEndTime());
            }
        }
        return queryWrapper;
    }

    /**
     * 添加新经费数据
     * @param financeInfo
     * @return
     */
    @ApiOperation(value = "添加新经费数据",notes = "添加新经费数据")
    @PostMapping("/addFinanceInfo")
    public Result addFinanceInfo(@RequestBody FinanceInfo financeInfo) {
        try{
            financeInfoService.addFinanceInfo(financeInfo);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }

    /**
     * 修改新经费数据
     * @param financeInfo
     * @return
     */
    @ApiOperation(value = "修改新经费数据",notes = "修改新经费数据")
    @PutMapping("/updateFinanceInfo")
    public Result updateFinanceInfo(@RequestBody FinanceInfo financeInfo) {
        try{
            financeInfoService.updateFinanceInfo(financeInfo);
            return Result.ok();
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }

    /**
     * ID查询单个新经费数据
     * @param id
     * @return
     */
    @GetMapping("/findFinanceInfo/{id}")
    @ApiOperation(value = "ID查询单个新经费数据",notes = "ID查询新经费数据")
    public Result findFinanceInfoById(@PathVariable("id") Integer id){
        FinanceInfo financeInfo = financeInfoService.getById(id);
        if (Objects.nonNull(financeInfo)){
            return Result.ok().data("financeInfo", financeInfo);
        }
        else {
            throw new BusinessException(ResultCode.FinanceInfo_NOT_EXIST.getCode(),ResultCode.FinanceInfo_NOT_EXIST.getMessage());
        }
    }

    /**
     * 删除单个新经费数据
     * @param id
     * @return Result
     */
    @ApiOperation(value = "删除单个新经费数据",notes = "根据id删除单个新经费数据")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        try{
            financeInfoService.removeById(id);
            return Result.ok();
        }catch (Exception e){
            throw new BusinessException(ResultCode.FinanceInfo_NOT_EXIST.getCode(),ResultCode.FinanceInfo_NOT_EXIST.getMessage());
        }
    }

    /**
     * 对比饼图
     * @return Result
     */
    @ApiOperation(value = "对比饼图",notes = "对比饼图接口")
    @GetMapping("/Compared")
    public List<ComparedVo> ComparedVoList(){
        return this.financeInfoService.ComparedVoList();
    }


    /**
     * 历史折线图
     * @return
     */
    @GetMapping("/history")
    @ApiOperation(value = "历史折线图",notes = "查询历史折线图")
    public Result getHistory(){
        List<HistoryVo> history = this.financeInfoService.getHistories();
        return Result.ok().data("history",history);
    }
}

